import { App } from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
import { createGuard } from './modules/guard'

export const router = createRouter({
    history: createWebHashHistory(),
    routes: constRoutes,
    // 每次刷新重置滚动条位置
    scrollBehavior: () => ({ left: 0, top: 0 }),
})

export const registerRouter = async (app: App) => {
    const { admin, initAuthRoute, getAuthRoutes, adminUserLogout } = useAdminStore()

    // 动态路由刷新白屏，需要在路由注册之前重新添加动态路由
    if (admin.accessToken) {
        await getAuthRoutes()
        initAuthRoute()
    } else {
        adminUserLogout()
    }

    app.use(router)

    createGuard(router)
}
